<?php
/**
 * @file name : menuheader_model.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Menuheader_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * @function assignMenuAdministrator
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   public
     * @param     array
     * @param     string
     */
    public function assignMenuAdministrator($arrModule = array(), $projectName)
    {
        // Reader file default
        $filename = APPPATH . 'control_default/base_controller_default.php';
        $fp = fopen($filename, 'r');
        $buff = '';
        if ($fp) {
            while ( ! feof($fp)) {
                $buff .= fgets($fp);
            }
        }
        fclose($fp);

        $filename = PROJECT_PATH . $projectName . '/administrator/application/libraries/Base_Controller.php';
        $fp = fopen($filename, 'w');
        if ($fp) {
            $strAssign = '';
            foreach ($arrModule as $key => $module) {
                if ( ! is_null($module->m_tbl_name) && strlen($module->m_tbl_name) > 0) {
                    $modeLink = $module->m_name . '_link';
                    $modeControl = preg_replace('/_/', '', $module->m_name);
                    $strAssign .= '        '
                               .  '$this->msmarty->assign(\'' . $modeLink . '\', \'?c=' . $modeControl . '\');' . "\n";
                } else {
                    // Assign for child module menu
                    $arrWhere = array($module->m_id);
                    $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                    foreach ($arrChildModule as $childKey => $childModule) {
                        $modeLink = $childModule->m_name . '_link';
                        $modeControl = preg_replace('/_/', '', $childModule->m_name);
                        $strAssign .= '        '
                                   .  '$this->msmarty->assign(\'' . $modeLink . '\', \'?c=' . $modeControl . '\');' . "\n";
                    }
                }
            }
            if ($strAssign != '') {
                $buffer = sprintf($buff, $projectName, date('Y'), $strAssign);
                fwrite($fp, $buffer);
            }
        }
        fclose($fp);
    }
    /**
     * @function createMenuHeaderAdmin
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   public
     * @param    array
     * @param     array
     * @param    string
     */
    public function createMenuHeaderAdmin($arrGroupMode = array(), $arrModule = array(), $projectName,
                                          $tempKind = 'smartAdmin')
    {
        /**
         * Kiem tra trong group co nhung module nao
         */
        foreach ($arrGroupMode as $k => $group) {
            $arrGroupIdList = explode(',', $group->m_id);
            $arrPush = array();
            $arrObj  = array();
            foreach ($arrModule as $key => $module) {
                if (is_object($module)) {
                    $grKey = array_search($module->m_id, $arrGroupIdList);
                    if (is_numeric($grKey) && $grKey >= 0) {
                        array_push($arrPush, $arrGroupIdList[$grKey]);
                        $mode_id = 'm_' . $module->m_id;
                        $group->$mode_id = $module;
                        $arrModule[$key] = '';
                    }
                }
            }
            if (count($arrPush) > 0) {
                $group->m_id = implode(',', $arrPush);
            } else {
                $arrGroupMode[$k] = '';
            }
        }
        // Read header file
        if ($tempKind == 'T') { // Titanium
            $filename = APPPATH . 'template_titaninum/header.php';
        } else {
            $filename = APPPATH . 'tem_default/header_default.php';
        }
        $fp = fopen($filename, 'r');
        $buff = '';
        if ($fp) {
            while ( ! feof($fp)) {
                $buff .= fgets($fp);
            }
        }
        fclose($fp);
        /***********************/
        if ($tempKind == 'T') { // Titanium
            $strAssign = $this->createAdminMenuTitaniumTemplate($arrGroupMode, $arrModule)
        } else {
            $strAssign = $this->createAdminMenuSmartAdminTemplate($arrGroupMode, $arrModule);
        }
        // Create header file
        $filename = PROJECT_PATH . $projectName . '/administrator/application/templates/header.tpl';
        $fp = fopen($filename, 'w');
        if ($fp) {
            if ($strAssign != '' && $buff != '') {
                $buff = sprintf($buff, $projectName, $strAssign);
                fwrite($fp, $buff);
            }
        }
        fclose($fp);
    }
    /**
     * @function    createAdminMenuSmartAdminTemplate
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   public
     * @param    array
     * @param     array
     * @param    string
     */
    private function createAdminMenuSmartAdminTemplate($arrGroupMode, $arrModule)
    {
        $strAssign = '';
        $i = 0;
        // create group module menu
        foreach ($arrGroupMode as $k => $group) {
            if (is_object($group)) {
                $selCls = '';
                $i = $k + 1;
                if ($i == 1) {
                    $selCls = 'selected';
                }
                $arrModeId = explode(',', $group->m_id);
                $modeControl = $group->g_name;
                $strAssign .= '      '
                           .  '<li class="' . $selCls . '" id="s%d_1"> <span class="selected-arrow">&nbsp;</span>' . "\n"
                           .  '        '
                           .  '<a href="#s%d" class="paren">' . $modeControl . '</a>' . "\n"
                           .  '        '
                           .  '<ul class="sublinks" id="s%d">' . "\n";
                foreach ($arrModeId as $idx => $mId) {
                    $mode_id = 'm_' . $mId;
                    if (is_object($group->$mode_id)) {
                        if (! is_null($group->$mode_id->m_tbl_name) && strlen($group->$mode_id->m_tbl_name) > 0) {

                            $modeLink    = $group->$mode_id->m_name . '_link';
                            $modeControl = $group->$mode_id->m_name_view;
                            $strAssign .= '          '
                                       .  '<li class="selected"><a href="{$' . $modeLink . '}">##MANAGEMENT## '
                                       .  $modeControl
                                       .  '</a></li>' ."\n";
                        } else {
                            // Create child module
                            $arrWhere = array($group->$mode_id->m_id);
                            $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                            foreach ($arrChildModule as $childKey => $childModule) {
                                if ( ! is_null($childModule->m_tbl_name) && strlen($childModule->m_tbl_name) > 0) {
                                    $modeLink    = $childModule->m_name . '_link';
                                    $modeControl = $childModule->m_name_view;
                                    $strAssign .= '          '
                                               .  '<li class="selected"><a href="{$' . $modeLink . '}">##MANAGEMENT## '
                                               .  $modeControl
                                               .  '</a></li>' ."\n";
                                }
                            }
                        }
                    }
                }
                $strAssign .= '        '
                           .  '</ul>'
                           .  '      '
                           .  '</li>' . "\n";
                $strAssign = sprintf($strAssign, $i, $i, $i);
            }
        }
        // create module menu
        foreach ($arrModule as $key => $module) {
            if (is_object($module)) {
                $modeLink = $module->m_name . '_link';
                $modeControl = $module->m_name_view;
                if (! is_null($module->m_tbl_name) && strlen($module->m_tbl_name) > 0) {
                    $selCls = '';
                    if ($i == 0) {
                        $i = $key + 1;
                    } else {
                        $i++;
                    }
                    if ($i == 1) {
                        $selCls = 'selected';
                    }

                    $strAssign .= '      '
                               .  '<li class="' . $selCls . '" id="s%d_1"> <span class="selected-arrow">&nbsp;</span>' . "\n"
                               .  '        '
                               .  '<a href="#s%d" class="paren">' . $modeControl . '</a>' . "\n"
                               .  '        '
                               .  '<ul class="sublinks" id="s%d">' . "\n"
                               .  '          '
                               .  '<li class="selected"><a href="{$' . $modeLink . '}">##MANAGEMENT## ' . $modeControl
                               .  '</a></li>' ."\n"
                               .  '        '
                               .  '</ul>'
                               .  '      '
                               .  '</li>' . "\n";
                    $strAssign = sprintf($strAssign, $i, $i, $i);
                } else {
                    // for child menu
                    $selCls = '';
                    if ($i == 0) {
                        $i = $key + 1;
                    } else {
                        $i++;
                    }
                    if ($i == 1) {
                        $selCls = 'selected';
                    }
                    $strAssign .= '      '
                               .  '<li class="' . $selCls . '" id="s%d_1"> <span class="selected-arrow">&nbsp;</span>' . "\n"
                               .  '        '
                               .  '<a href="#s%d" class="paren">' . $modeControl . '</a>' . "\n"
                               .  '        '
                               .  '<ul class="sublinks" id="s%d">' . "\n";

                    $arrWhere = array($module->m_id);
                    $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                    foreach ($arrChildModule as $childKey => $childModule) {
                        if (! is_null($childModule->m_tbl_name) && strlen($childModule->m_tbl_name) > 0) {
                            $childModeLink    = $childModule->m_name . '_link';
                            $childModeControl = $childModule->m_name_view;
                            $strAssign .= '          '
                                       .  '<li class="selected"><a href="{$' . $childModeLink . '}">##MANAGEMENT## '
                                       .  $childModeControl
                                       .  '</a></li>' ."\n";
                        }
                    }
                    $strAssign .= '        '
                               .  '</ul>'
                               .  '      '
                               .  '</li>' . "\n";
                    $strAssign = sprintf($strAssign, $i, $i, $i);
                }
            }
        }
        return $strAssign;
    }
    /**
     * @function    createAdminMenuTitaniumTemplate
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   public
     * @param    array
     * @param     array
     * @param    string
     */
    private function createAdminMenuTitaniumTemplate($arrGroupMode, $arrModule)
    {
        $strAssign = '';
        $i = 0;
        // create group module menu
        foreach ($arrGroupMode as $k => $group) {
            if (is_object($group)) {
                $selCls = 'inactive';
                $expand = '';
                $i = $k + 1;
                if ($i == 1) {
                    $selCls = 'active';
                    $expand = ' expanded';
                }
                $arrModeId = explode(',', $group->m_id);
                $modeControl = $group->g_name;
                $strAssign .= '<li class="' . $selCls . '">' . "\n"
                           .  '                        '
                           .  '<a href="#" title="' . $modeControl . '" class="button">' . "\n"
                           .  '                        '
                           .  '    <strong>' . "\n" . '                        '
                           .  '        <img SRC="img/icons/newspaper_48.png" alt="comments" class="icon "/>' . "\n"
                           .  '                        '
                           .  '        <span class="title">' . $modeControl . '</span>' . "\n"
                           .  '                        '
                           .  '        <span class="expand' . $expand . '"></span>' . "\n"
                           .  '                        '
                           .  '    </strong>' . "\n" . '                        '
                           .  '</a>' . "\n"
                           .  '                        '
                           .  '<ul>' . "\n";
                foreach ($arrModeId as $idx => $mId) {
                    $mode_id = 'm_' . $mId;
                    if (is_object($group->$mode_id)) {
                        if (! is_null($group->$mode_id->m_tbl_name) && strlen($group->$mode_id->m_tbl_name) > 0) {

                            $modeLink    = $group->$mode_id->m_name . '_link';
                            $modeControl = $group->$mode_id->m_name_view;
                            $strAssign .= '                            '
                                       .  '<li><a href="{$' . $modeLink . '}">##MANAGEMENT## ' . $modeControl .  '</a></li>' ."\n";
                        } else {
                            // Create child module
                            $arrWhere = array($group->$mode_id->m_id);
                            $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                            foreach ($arrChildModule as $childKey => $childModule) {
                                if ( ! is_null($childModule->m_tbl_name) && strlen($childModule->m_tbl_name) > 0) {
                                    $modeLink    = $childModule->m_name . '_link';
                                    $modeControl = $childModule->m_name_view;
                                    $strAssign .= '                            '
                                               .  '<li><a href="{$' . $modeLink . '}">##MANAGEMENT## '
                                               .  $modeControl
                                               .  '</a></li>' ."\n";
                                }
                            }
                        }
                    }
                }
                $strAssign .= '        '
                           .  '</ul>'
                           .  '      '
                           .  '</li>' . "\n";
            }
        }
        // create module menu
        foreach ($arrModule as $key => $module) {
            if (is_object($module)) {
                $modeLink = $module->m_name . '_link';
                $modeControl = $module->m_name_view;
                if (! is_null($module->m_tbl_name) && strlen($module->m_tbl_name) > 0) {
                    $selCls = 'inactive';
                    $expand = '';
                    $i = $key + 1;
                    if ($i == 1) {
                        $selCls = 'active';
                        $expand = ' expanded';
                    }

                    $strAssign .= '      '
                               .  '<li class="' . $selCls . '">' . "\n"
                               .  '                        '
                               .  '<a href="#" title="' . $modeControl . '" class="button">' . "\n"
                               .  '                        '
                               .  '    <strong>' . "\n" . '                        '
                               .  '        <img SRC="img/icons/newspaper_48.png" alt="comments" class="icon "/>' . "\n"
                               .  '                        '
                               .  '        <span class="title">' . $modeControl . '</span>' . "\n"
                               .  '                        '
                               .  '        <span class="expand' . $expand . '"></span>' . "\n"
                               .  '                        '
                               .  '    </strong>' . "\n" . '                        '
                               .  '</a>' . "\n"
                               .  '                        '
                               .  '<ul>' . "\n"
                               .  '                        '
                               .  '    <li><a href="{$' . $modeLink . '}">##MANAGEMENT## ' . $modeControl .  '</a></li>' ."\n"
                               .  '                        '
                               .  '</ul>' . "\n"
                               .  '                    '
                               .  '</li>' . "\n";
                } else {
                    // for child menu
                    $selCls = 'inactive';
                    $expand = '';
                    if ($i == 0) {
                        $i = $key + 1;
                        $selCls = 'active';
                        $expand = ' expanded';
                    }
                    $strAssign .= '      '
                               .  '<li class="' . $selCls . '">' . "\n"
                               .  '                        '
                               .  '<a href="#" title="' . $modeControl . '" class="button">' . "\n"
                               .  '                        '
                               .  '    <strong>' . "\n" . '                        '
                               .  '        <img SRC="img/icons/newspaper_48.png" alt="comments" class="icon "/>' . "\n"
                               .  '                        '
                               .  '        <span class="title">' . $modeControl . '</span>' . "\n"
                               .  '                        '
                               .  '        <span class="expand' . $expand . '"></span>' . "\n"
                               .  '                        '
                               .  '    </strong>' . "\n" . '                        '
                               .  '</a>' . "\n"
                               .  '                        '
                               .  '<ul>' . "\n"
                               .  '                        '
                               .  '<ul>' . "\n";

                    $arrWhere = array($module->m_id);
                    $arrChildModule = $this->modules_model->loadListIn('parent_id', $arrWhere);
                    foreach ($arrChildModule as $childKey => $childModule) {
                        if (! is_null($childModule->m_tbl_name) && strlen($childModule->m_tbl_name) > 0) {
                            $childModeLink    = $childModule->m_name . '_link';
                            $childModeControl = $childModule->m_name_view;
                            $strAssign .= '                            '
                                       .  '<li><a href="{$' . $childModeLink . '}">##MANAGEMENT## '
                                       .  $childModeControl
                                       .  '</a></li>' ."\n";
                        }
                    }
                    $strAssign .= '                        '
                               .  '</ul>'
                               .  '                    '
                               .  '</li>' . "\n";
                }
            }
        }
        return $strAssign;
    }
}